package nc.ui.medde.vocodegen;

import java.util.ArrayList;
import java.util.Iterator;

import nc.md.gen.CodeGenException;
import nc.md.gen.CodeGenUtils;
import nc.md.gen.generator.ICodeGenerator;
import nc.md.gen.vmobj.ExAggVMObject;
import nc.md.gen.vmobj.IVMObject;
import nc.ui.medde.actions.menu.vocreate.CodeSwitch;
import nc.vo.database.support.Table;

/**
 * @author yany
 * 
 */
public class UapdeExAggVoGenerator extends UapdeAbstractGenerator {

	private ICodeGenerator sVOgen;

	public UapdeExAggVoGenerator(String destFolder, CodeSwitch codeSwitch,
			Table t) {
		super(destFolder, codeSwitch, t);
		setVmfile(EXAGGREGATED);
	}

	@Override
	protected IVMObject convertVMObject(Object bean) throws CodeGenException {
		Table table = (Table) bean;
		ExAggVMObject vmobj = new ExAggVMObject();
		BaseVmObjectUtil.fillBaseInfo(vmobj, getCodeSwitch());
		String voName = getVoName(table);
		String[] pkgclz = CodeGenUtils.unCombineQualifiedClassname(voName);
		vmobj.setQualifiedName(pkgclz[0] + ".ExAgg" + pkgclz[1]);

		ArrayList<String> strCodeList = new ArrayList<String>();
		ArrayList<String> strNameList = new ArrayList<String>();
		Iterator itChildTable = table.getChildTables().iterator();
		while (itChildTable.hasNext()) {
			Table childTable = (Table) itChildTable.next();
			strCodeList.add(childTable.getName());
			String nameCN = childTable.getName_cn();
			if (nameCN == null) {
				nameCN = childTable.getName();
			}
			strNameList.add(nameCN);
		}

		vmobj.setTableCodeList(strCodeList);
		vmobj.setTableNameList(strNameList);
		vmobj.setParentvo(voName);
		return vmobj;
	}

	public void generateCode() throws CodeGenException {
		sVOgen = new UapdeSVoGenerator(destFolder, getCodeSwitch(),
				(Table) bean);
		sVOgen.generateCode();
		IVMObject vmobj = convertVMObject(bean);
		generate(vmobj);
	}

}
